#include "choosedatastructure.h"
#include "avl.h"
#include "trie.h"
#include "hush.h"
#include "mainwindow.h"
#include <bits/stdc++.h>
using namespace std;
#include <QMenuBar>
#include <QPainter>
#include<QPushButton>
#include <QFont>
#include <QTimer>
#include <QMessageBox>

ChooseDataStructure::ChooseDataStructure(QWidget *parent) : QMainWindow(parent) {
    //配置一下
    this->setFixedSize(800, 588);
    //设置图标
    this->setWindowIcon(QIcon(":/res/cidian.jpg"));
    //设置标题
    this->setWindowTitle("选择相应的数据结构");
    //创建菜单栏
    QMenuBar *bar = menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu *startMenu = bar->addMenu("开始");
    //创建退出
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction, &QAction::triggered, [=]() {
       this->close();
    });
    //back按钮
//    QPushButton *back = new QPushButton("BACK");
//    back->setParent(this);
//    back->resize(100, 50);
//    back->move(QPoint(this->width() - back->width(), this->height() - back->height()));
//    back->show();
//    connect(back, &QPushButton::clicked, [=]() {

//    });
      //三种数据结构的相应按钮
      //字体
      QFont font("Microsoft YaHei", 10, 75);
      QPushButton *avlPushButton = new QPushButton("AVL数据结构");
      avlPushButton->setParent(this);
      avlPushButton->move(QPoint(300, 100));
      avlPushButton->resize(150, 50);
      avlPushButton->show();
      avlPushButton->setFlat(true);
      avlPushButton->setFont(font);
      QPushButton *triePushButton = new QPushButton("Trie数据结构");
      triePushButton->setParent(this);
      triePushButton->move(QPoint(300, 300));
      triePushButton->resize(150, 50);
      triePushButton->show();
      triePushButton->setFlat(true);
      triePushButton->setFont(font);
      QPushButton *hushPushButton = new QPushButton("Hush数据结构");
      hushPushButton->setParent(this);
      hushPushButton->resize(150, 50);
      hushPushButton->move(300, 500);
      hushPushButton->show();
      hushPushButton->setFlat(true);
      hushPushButton->setFont(font);
      //三种数据结构进行连接
      mainwindow = new MainWindow;



      //avl 按钮
      connect(avlPushButton, &QPushButton::clicked, [=]() {
         //延迟进入
          QTimer::singleShot(200, this, [=]() {
              //进入到相关页面 并且将自身隐藏
              AVL *test = new AVL();
              this->hide();
              mainwindow->show();

              //插入操作与AVL树的连接
              connect(mainwindow->getInsertButton(), &QPushButton::clicked, [=]() {
                  freopen("C:\\Users\\86156\\Desktop\\QT projects\\NewDataStructure\\English\\all.txt", "r", stdin);
                  string a, b;
                  while(cin >> a >> b) {
                    test->insert(a, b);
                    qDebug() << QString::fromStdString(a) << ' ' << QString::fromStdString(b);
                  }
                  QMessageBox::information(this, "插入单词操作", "AVL数据结构已经插入所有单词");
              });

              //删除操作与AVL树的连接
              connect(mainwindow->getDeleteButton(), &QPushButton::clicked, [=]() {
                 QString res = mainwindow->getWord2()->text();
                 string temp = res.toStdString();
                 test->deleteNode(temp);
                 if(test->res == "未找到该单词") {
                     QMessageBox::information(this, "删除单词操作", "字典中不存在该单词");
                     mainwindow->getDeleteEdit()->setText("未找到该单词");
                 } else {
                     QMessageBox::information(this, "删除单词操作", QString::fromStdString(temp) + "已经删除");
                     mainwindow->getDeleteEdit()->setText("删除成功");
                 }
              });

              //查找操作与AVL树的连接
              connect(mainwindow->getTranslateButton(), &QPushButton::clicked, [=]() {
                 QString res = mainwindow->getWord1()->text();
                 string temp = res.toStdString();
                 test->search(temp);
                 QMessageBox::information(this, "查找操作", "单词已经查找完成");
                 mainwindow->getTranslateEdit()->setText(QString::fromStdString(test->res));
              });

          });
      });



      //trie按钮
      connect(triePushButton, &QPushButton::clicked, [=]() {
          //延迟进入
           QTimer::singleShot(200, this, [=]() {
               //进入到相关页面 并且将自身隐藏
               Trie *test = new Trie();
               this->hide();
               mainwindow->show();

               //插入操作与Trie树的连接
               connect(mainwindow->getInsertButton(), &QPushButton::clicked, [=]() {
                   freopen("C:\\Users\\86156\\Desktop\\QT projects\\NewDataStructure\\English\\all.txt", "r", stdin);
                   string a, b;
                   while(cin >> a >> b) {
                     test->insert(a, b);
                     qDebug() << QString::fromStdString(a) << ' ' << QString::fromStdString(b);
                   }
                   QMessageBox::information(this, "插入单词操作", "Trie数据结构已经插入所有单词");
               });

               //删除操作与Trie树的连接
               connect(mainwindow->getDeleteButton(), &QPushButton::clicked, [=]() {
                  QString res = mainwindow->getWord2()->text();
                  string temp = res.toStdString();
                  if(test->search(temp) != "未找到该单词") {
                      test->deleteNode(temp);
                      mainwindow->getDeleteEdit()->setText("删除成功");
                      QMessageBox::information(this, "删除单词操作", QString::fromStdString(temp) + "已经删除");
                  } else {
                      QMessageBox::information(this, "删除单词操作", "字典中不存在该单词");
                      mainwindow->getDeleteEdit()->setText("未找到该单词");
                  }
               });

               //查找操作与Trie树的连接
               connect(mainwindow->getTranslateButton(), &QPushButton::clicked, [=]() {
                  QString res = mainwindow->getWord1()->text();
                  string temp = res.toStdString();
                  string str = test->search(temp);
                  QMessageBox::information(this, "查找操作", "单词已经查找完成");
                  mainwindow->getTranslateEdit()->setText(QString::fromStdString(str));
               });

           });
      });



      //hush按钮
      connect(hushPushButton, &QPushButton::clicked, [=]() {
         //延迟进入
          QTimer::singleShot(200, this, [=]() {
              //进入到相关页面 并且将自身隐藏
              Hush *test = new Hush();
              this->hide();
              mainwindow->show();

              //插入操作与Hush的连接
              connect(mainwindow->getInsertButton(), &QPushButton::clicked, [=]() {
                  freopen("C:\\Users\\86156\\Desktop\\QT projects\\NewDataStructure\\English\\all.txt", "r", stdin);
                  string a, b;
                  while(cin >> a >> b) {
                    test->insert(a, b);

                  }
                  QMessageBox::information(this, "插入单词操作", "Hush数据结构已经插入所有单词");
              });

              //删除操作与Hush的连接
              connect(mainwindow->getDeleteButton(), &QPushButton::clicked, [=]() {
                 QString res = mainwindow->getWord2()->text();
                 string temp = res.toStdString();
                 if(test->find(temp) != "未找到该单词") {
                     test->deleteKey(temp);
                     mainwindow->getDeleteEdit()->setText("删除成功");
                     QMessageBox::information(this, "删除单词操作", QString::fromStdString(temp) + "已经删除");
                 } else {
                     QMessageBox::information(this, "删除单词操作", "字典中不存在该单词");
                     mainwindow->getDeleteEdit()->setText("未找到该单词");
                 }
              });

              //查找操作与Hush的连接
              connect(mainwindow->getTranslateButton(), &QPushButton::clicked, [=]() {
                 QString res = mainwindow->getWord1()->text();
                 string temp = res.toStdString();
                 string str = test->find(temp);
                 QMessageBox::information(this, "查找操作", "单词已经查找完成");
                 mainwindow->getTranslateEdit()->setText(QString::fromStdString(str));
              });
          });
      });
}

void ChooseDataStructure::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bear.jpg");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);
}
